package com.yanqu.road.dao.impl.activity.starlight;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.starlight.StarLightMallRecord;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : Cxp
 * @Time : 2024-11-07
 * @Description : 商城补发记录Dao
 */

public class StarLightMallRecordDaoImpl extends TempDataObjectDao<StarLightMallRecord> {

    public List<StarLightMallRecord> getMallRecordsByUserId(int activityId, long userId) {
        List<StarLightMallRecord> res = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        int i = 1;
        String sql = "select * from t_u_starlight_mall_record_data where activityId=? and userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(i++, activityId);
            call.setLong(i, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                StarLightMallRecord data = parseData(rs);
                res.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }



    public List<StarLightMallRecord> getMallRecordsByType(int activityId, int acquireType) {
        List<StarLightMallRecord> res = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_starlight_mall_record_data where activityId=? and acquireType=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            call.setInt(2, acquireType);
            rs = call.executeQuery();
            while (rs.next()) {
                StarLightMallRecord data = parseData(rs);
                res.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(StarLightMallRecord data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_starlight_mall_record_data(activityId,userId,activityDay,mallId,buyType,acquireType," +
                "acquireTime)VALUES (?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getActivityDay());
            call.setInt(i++, data.getMallId());
            call.setInt(i++, data.getBuyType());
            call.setInt(i++, data.getAcquireType());
            call.setLong(i++, data.getAcquireTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(StarLightMallRecord data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_starlight_mall_record_data SET acquireType=?,acquireTime=? WHERE activityId=? and " +
                "userId=? and activityDay=? and mallId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getAcquireType());
            call.setLong(i++, data.getAcquireTime());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getActivityDay());
            call.setInt(i++, data.getMallId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected StarLightMallRecord parseData(ResultSet rs) throws SQLException {
        StarLightMallRecord data = new StarLightMallRecord();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setActivityDay(rs.getInt("activityDay"));
        data.setMallId(rs.getInt("mallId"));
        data.setBuyType(rs.getInt("buyType"));
        data.setAcquireType(rs.getInt("acquireType"));
        data.setAcquireTime(rs.getLong("acquireTime"));
        return data;
    }
}
